package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.controlserver.AndroidOrder;
import com.ruoyi.system.controlserver.ControlServer;
import com.ruoyi.system.controlserver.SmartInfo;
import com.ruoyi.system.domain.SrswDeviceInfo;
import com.ruoyi.system.domain.SrswDevices;
import com.ruoyi.system.mapper.SrswDevicesMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISmartScreenService;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenjy
 * @date 2024/12/3 15:38
 */
@Service
public class ISmartScreenServiceImpl implements ISmartScreenService {
    @Autowired
    private ControlServer controlServer;
    @Autowired
    private SrswDevicesMapper srswDevicesMapper;
    @Autowired
    private SysDeptMapper deptMapper;

    @Override
    public List<SmartInfo> list(SmartInfo info) {
        final Map<String, SmartInfo> channelMap = controlServer.getChannelMap();
        channelMap.values().removeIf(smartInfo -> {
            Channel channel = smartInfo.getChannel();
            if (channel == null || !channel.isActive()) {
                return true;
            }
            return false;
        });
        return new ArrayList<>(channelMap.values());
    }

    @Override
    public List<SmartInfo> newList() {
        List<SysDept> sysDeptList = deptMapper.selectDeptList(new SysDept());
        List<SmartInfo> smartInfoList = new ArrayList<>();
        for (SysDept dept : sysDeptList) {
            SmartInfo smartInfo = new SmartInfo();
            smartInfo.setId(dept.getDeptId()+"");
            smartInfo.setParentId(dept.getParentId()+"");
            smartInfo.setDeptName(dept.getDeptName());
            smartInfo.setOrderNum(dept.getOrderNum());
            smartInfoList.add(smartInfo);
        }

        SrswDevices srswDevices = new SrswDevices();
        List<SrswDevices> srswDevicesList = srswDevicesMapper.selectSrswDevicesList(srswDevices);
        Map<String, SmartInfo> channelMap = controlServer.getChannelMap();
        for (SrswDevices device : srswDevicesList) {
            SmartInfo smartInfo = channelMap.get(device.getDeviceNo());
            if (smartInfo == null) {
                smartInfo = new SmartInfo();
                smartInfo.setId(device.getDeviceNo());
                smartInfo.setParentId(device.getDeptId()+"");
                smartInfo.setUpTime(device.getUpTime());
                smartInfo.setDeviceName(device.getDeviceName());
                smartInfo.setOrderNum(0);
                smartInfo.setDeviceNo(device.getDeviceNo());
                smartInfo.setCreateTime(device.getCreateTime());
            }else{
                smartInfo.setId(device.getDeviceNo());
                smartInfo.setParentId(device.getDeptId()+"");
                smartInfo.setUpTime(device.getUpTime());
                smartInfo.setCreateTime(device.getCreateTime());
                smartInfo.setDeviceName(device.getDeviceName());
                smartInfo.setOrderNum(0);
                smartInfo.setOnline(true);
            }
            smartInfoList.add(0,smartInfo);
        }

        return smartInfoList;
    }

    @Override
    public int instruct(SmartInfo info) {
        final SmartInfo smartInfo = controlServer.getChannelByDeviceNo(info.getDeviceNo());
        if (smartInfo == null) {
            return 0;
        }
        String instruction;
        final Channel channel = smartInfo.getChannel();
        if (info.getVoice() != null) {
            instruction = "voice".concat(":").concat(String.valueOf(info.getVoice()));
            channel.writeAndFlush(instruction);
            return controlServer.instructResult(instruction);
        }
        if (info.getScreenStatus() != null) {
            String value = info.getScreenStatus() ? "on" : "off";
            final AndroidOrder order = AndroidOrder.getAndroidOrder("screen", value);
            if (order == null) {
                throw new ServiceException("无效的屏幕状态切换指令");
            }
            instruction = order.getType().concat(":").concat(order.getOrder());
            channel.writeAndFlush(instruction).addListener(future -> {
                if (future.isSuccess()) {
                    System.out.println("指令 " + instruction + " 发送成功");
                    return;
                }
                System.out.println("指令 " + instruction + " 发送失败");
            });
            return controlServer.instructResult(instruction);
        }
        if (info.getPowerStatus() != null) {
            final AndroidOrder order = AndroidOrder.POWEROFF;
            instruction = order.getType().concat(":").concat(order.getOrder());
            channel.writeAndFlush(instruction).addListener(future -> {
                if (future.isSuccess()) {
                    System.out.println("指令 " + instruction + " 发送成功");
                    return;
                }
                System.out.println("指令 " + instruction + " 发送失败");
            });
            return controlServer.instructResult(instruction);
        }
        if (info.getMuteStatus() != null) {
            String value = info.getMuteStatus() ? "on" : "off";
            final AndroidOrder order = AndroidOrder.getAndroidOrder("mute", value);
            if (order == null) {
                throw new ServiceException("无效的静音切换指令");
            }
            instruction = order.getType().concat(":").concat(order.getOrder());
            channel.writeAndFlush(instruction).addListener(future -> {
                if (future.isSuccess()) {
                    System.out.println("指令 " + instruction + " 发送成功");
                    return;
                }
                System.out.println("指令 " + instruction + " 发送失败");
            });
            return controlServer.instructResult(instruction);
        }
        if (info.getRestart() != null) {
            final AndroidOrder order = AndroidOrder.RESTART;
            instruction = order.getType().concat(":").concat(order.getOrder());
            channel.writeAndFlush(instruction);
            return controlServer.instructResult(instruction);
        }
        if (info.getSource() != null) {
            final AndroidOrder order = AndroidOrder.getAndroidOrder("source", info.getSource());
            if (order == null) {
                throw new ServiceException("无效的信号源切换指令");
            }
            instruction = order.getType().concat(":").concat(order.getOrder());
            channel.writeAndFlush(instruction).addListener(future -> {
                if (future.isSuccess()) {
                    System.out.println("指令 " + info.getSource() + " 发送成功");
                    return;
                }
                System.out.println("指令 " + info.getSource() + " 发送失败");
            });
            return controlServer.instructResult(instruction);
        }
        final Boolean opsStatus = info.getOpsStatus();
        if (opsStatus != null) {
            String value = opsStatus ? "on" : "off";
            final AndroidOrder order = AndroidOrder.getAndroidOrder("ops", value);
            if (order == null) {
                throw new ServiceException("无效的pc切换指令");
            }
            instruction = order.getType().concat(":").concat(order.getOrder());
            channel.writeAndFlush(instruction).addListener(future -> {
                if (future.isSuccess()) {
                    System.out.println("指令 " + info.getOpsStatus() + " 发送成功");
                    return;
                }
                System.out.println("指令 " + info.getOpsStatus() + " 发送失败");
            });
            return controlServer.instructResult(instruction);
        }
        throw new ServiceException("无效指令");
    }
}
